package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAdministrativeMapper;
import com.ruoyi.system.domain.SysAdministrative;
import com.ruoyi.system.service.ISysAdministrativeService;

/**
 * 行政区Service业务层处理
 * 
 * @author sw
 * @date 2025-10-19
 */
@Service
public class SysAdministrativeServiceImpl implements ISysAdministrativeService 
{
    @Autowired
    private SysAdministrativeMapper sysAdministrativeMapper;

    /**
     * 查询行政区
     * 
     * @param administrativeId 行政区主键
     * @return 行政区
     */
    @Override
    public SysAdministrative selectSysAdministrativeByAdministrativeId(Long administrativeId)
    {
        return sysAdministrativeMapper.selectSysAdministrativeByAdministrativeId(administrativeId);
    }

    /**
     * 查询行政区列表
     * 
     * @param sysAdministrative 行政区
     * @return 行政区
     */
    @Override
    public List<SysAdministrative> selectSysAdministrativeList(SysAdministrative sysAdministrative)
    {
        return sysAdministrativeMapper.selectSysAdministrativeList(sysAdministrative);
    }

    /**
     * 新增行政区
     * 
     * @param sysAdministrative 行政区
     * @return 结果
     */
    @Override
    public int insertSysAdministrative(SysAdministrative sysAdministrative)
    {
        sysAdministrative.setCreateTime(DateUtils.getNowDate());
        return sysAdministrativeMapper.insertSysAdministrative(sysAdministrative);
    }

    /**
     * 修改行政区
     * 
     * @param sysAdministrative 行政区
     * @return 结果
     */
    @Override
    public int updateSysAdministrative(SysAdministrative sysAdministrative)
    {
        sysAdministrative.setUpdateTime(DateUtils.getNowDate());
        return sysAdministrativeMapper.updateSysAdministrative(sysAdministrative);
    }

    /**
     * 批量删除行政区
     * 
     * @param administrativeIds 需要删除的行政区主键
     * @return 结果
     */
    @Override
    public int deleteSysAdministrativeByAdministrativeIds(Long[] administrativeIds)
    {
        return sysAdministrativeMapper.deleteSysAdministrativeByAdministrativeIds(administrativeIds);
    }

    /**
     * 删除行政区信息
     * 
     * @param administrativeId 行政区主键
     * @return 结果
     */
    @Override
    public int deleteSysAdministrativeByAdministrativeId(Long administrativeId)
    {
        return sysAdministrativeMapper.deleteSysAdministrativeByAdministrativeId(administrativeId);
    }

    /**
     * 通过集合获取树结构，递归查询
     * @param list
     * @return
     */
    @Override
    public List<SysAdministrative> listAdministrativeByTree(List<SysAdministrative> list) {
        // 构建树结构
        List<SysAdministrative> treeList = new ArrayList<>();
        // 遍历集合，为每个元素查找子节点
        for (SysAdministrative administrative : list) {
            // 查询当前行政区的子行政区
            SysAdministrative query = new SysAdministrative();
            query.setParentCode(administrative.getAdministrativeCode());
            List<SysAdministrative> children = selectSysAdministrativeList(query);

            // 递归构建子节点树结构
            if (children != null && !children.isEmpty()) {
                administrative.setChildren(listAdministrativeByTree(children));
            }

            treeList.add(administrative);
        }

        return treeList;
    }
}
